package com.qs.home.ui.VM;

import android.app.Application;
import android.content.Intent;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baidu.ocr.demo.FileUtil;
import com.baidu.ocr.demo.MainActivity;
import com.baidu.ocr.ui.camera.CameraActivity;
import com.google.gson.Gson;
import com.qs.base.global.SPKeyGlobal;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.utils.BaseTimeUtils;

import java.util.Date;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 证件识别vm
 *
 * @Author ltzz
 * @Date 2019/8/13
 */
public class CertificateRecognitionViewModel extends BaseViewModel {
    public CertificateRecognitionViewModel(@NonNull Application application) {
        super(application);
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //证件识别
        public ObservableField<String> credentialsUc = new ObservableField<String>("");
        //驾驶证识别
        public ObservableBoolean photoDiscernUc = new ObservableBoolean(false);
    }

    private Disposable subscribe;
    //身份证扫描
    public BindingCommand identityCardClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ARouter.getInstance().build(RouterActivityPath.Home.ID_CARD).navigation();
        }
    });
    //银行卡扫描
    public BindingCommand bankCardClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (subscribe != null && !subscribe.isDisposed())
                subscribe.dispose();
            subscribe = RxBus.getDefault().toObservable(String.class)
                    .subscribe(new Consumer<String>() {
                        @Override
                        public void accept(String result) throws Exception {
                            KLog.e("银行卡识别：" + result);
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            //是否会员
                            boolean isOk = false;
                            if (!StringUtils.isTrimEmpty(SPUtils.getInstance().getString(SPKeyGlobal.USER_ID))) {
                                //用户已登录
                                //查看是否是会员
                                String date = SPUtils.getInstance().getString(SPKeyGlobal.USER_DATE, "");
                                if (!StringUtils.isTrimEmpty(date)) {
                                    if (Integer.parseInt(date) > 0) {
                                        isOk = true;
                                    }
                                }
                            }
                            if (!isOk) {
                                String seeNumberStr = SPUtils.getInstance().getString(SPKeyGlobal.SEE_NUMBER, "0");
                                int x = Integer.parseInt(seeNumberStr);
                                if (x <= 0) {
                                    ToastUtils.showShort("识别次数已用完");
                                    return;
                                }
                            }

                            //消耗识别次数
                            String seeNumberStr = SPUtils.getInstance().getString(SPKeyGlobal.SEE_NUMBER, "0");
                            int x = Integer.parseInt(seeNumberStr);
                            SPUtils.getInstance().put(SPKeyGlobal.SEE_NUMBER, ((x - 1) < 0 ? 0 : (x - 1)) + "");
                            ARouter.getInstance().build(RouterActivityPath.Home.BANK_CARD_RESULTS_ACTIVITY).withString("textResult", result).navigation();
                        }
                    });
            RxSubscriptions.add(subscribe);
            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath())
                    .withString(CameraActivity.KEY_CONTENT_TYPE + "C", CameraActivity.CONTENT_TYPE_BANK_CARD)
                    .withInt("identificationType", 120)
                    .navigation();
        }
    });
    //学位证扫描
    public BindingCommand degreeCertificateClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("学位证扫描");
        }
    });
    //驾驶证扫描
    public BindingCommand drivingLicenceClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (subscribe != null && !subscribe.isDisposed())
                subscribe.dispose();
            subscribe = RxBus.getDefault().toObservable(String.class)
                    .subscribe(new Consumer<String>() {
                        @Override
                        public void accept(String result) throws Exception {
                            KLog.e("驾驶证识别路径：" + new Gson().toJson(result));
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                            KLog.e("拍照识别路径：" + result);
                            uc.photoDiscernUc.set(!uc.photoDiscernUc.get());
                            if (subscribe != null && !subscribe.isDisposed())
                                subscribe.dispose();
                        }
                    });
            RxSubscriptions.add(subscribe);
            ARouter.getInstance().build(RouterActivityPath.OCR.CAMERA_ACTIVITY)
                    .withString(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath())
                    .withString(CameraActivity.KEY_CONTENT_TYPE + "C", CameraActivity.CONTENT_TYPE_GENERAL)
                    .navigation();

        }
    });

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (subscribe != null && !subscribe.isDisposed())
            subscribe.dispose();
    }
}
